package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.RedisConstant;
import com.itheima.dao.SetmealDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import redis.clients.jedis.JedisPool;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author qzh
 * @create 2020-01-09 2020/1/9
 */
@Service(interfaceClass = SetmealService.class)
@Transactional
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealDao setmealDao;

    @Autowired
    private JedisPool jedisPool;

    @Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer;

//    @Value("${out_put_path}")
    private String outPutPath ="E:/itcast_health/health_parent/health_mobile/src/main/webapp/pages";

    @Override
    public void add(Setmeal setmeal, Integer[] checkgroupIds) {
        //新增套餐
        setmealDao.addSetmeal(setmeal);
        //新增套餐和检查组之间的关系
        for (Integer checkgroupId : checkgroupIds) {
            setmealDao.addSetmealAndCheckGroupRel(setmeal.getId(),checkgroupId);
        }
        String fileName = setmeal.getImg();
        if(fileName!=null) {
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES, fileName);
        }

        //当添加套餐后需要生成静态页面(套餐列表页面，套餐详情页面)
        generateMobileStaticHtml();

    }


    //生成当前方法所需的静态页面
    public void generateMobileStaticHtml(){

        //生成静态页面之前需要查询数据
        List<Setmeal> all = setmealDao.findAll();



        //生成套餐列表静态页面
        generateMobileSetmealListHtml(all);
        //生成套餐详情静态页面
        generateMobileSetmealDetailHtml(all);


    }

    //生成套餐列表静态页面
    public void generateMobileSetmealListHtml(List<Setmeal> list){
        Map map = new HashMap();
        map.put("setmealList",list);
        for (Setmeal setmeal : list) {
            System.out.println(setmeal.getImg());
        }
        generateHtml("mobile_setmeal.ftl","m_setmeal.html",map);
    }

    //生成套餐详情静态页面

    public void generateMobileSetmealDetailHtml(List<Setmeal> list){
        for (Setmeal setmeal : list) {
            Map map = new HashMap();
            map.put("setmeal",setmealDao.findCheckItemCheckGroupSetmealById(setmeal.getId()));
            generateHtml("mobile_setmeal_detail.ftl","setmeal_detail_"+setmeal.getId()+".html",map);
        }



    }



















    //用于生成静态页面
    public void generateHtml(String teplateName, String htmlPageName, Map map){

        //1.
        Configuration configuration = freeMarkerConfigurer.getConfiguration();
        //2.
        Writer writer = null;
        try {
            Template template = configuration.getTemplate(teplateName);
            writer = new FileWriter(new File(outPutPath + "/"+ htmlPageName));
            template.process(map,writer);

        } catch (IOException e) {
            e.printStackTrace();
        } catch (TemplateException e) {
            e.printStackTrace();
        }


    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {

        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        if(queryString!=null){
            queryString = "%"+queryString+"%";
        }
        PageHelper.startPage(currentPage,pageSize);
        Page<Setmeal> setmealPage = setmealDao.findPage(queryPageBean);
        List<Setmeal> result = setmealPage.getResult();
        long total = setmealPage.getTotal();
        PageResult pageResult = new PageResult(total, result);


        return pageResult;
    }

    @Override
    public Setmeal findById(Integer id) {

        return setmealDao.findById(id);


    }

    @Override
    public List<Integer> findCheckGroupIdsBySetmealId(Integer id) {

        return setmealDao.findCheckGroupIdsBySetmealId(id);

    }

    @Override
    public void edit(Setmeal setmeal, Integer[] checkgroupIds) {

        //修改套餐基本信息
        setmealDao.edit(setmeal);

        //删除关系  根据setmealId
        setmealDao.deleteSetmealAndCheckGroupRel(setmeal.getId());

        //重新建立关系
        for (Integer checkgroupId : checkgroupIds) {
            setmealDao.addSetmealAndCheckGroupRel(setmeal.getId(),checkgroupId);
        }


    }

    @Override
    public void delete(Integer id) {

        setmealDao.deleteSetmealAndCheckGroupRel(id);
        setmealDao.deleteSetmealById(id);


    }

    @Override
    public List<Setmeal> findAll() {

        List<Setmeal> all = setmealDao.findAll();
        return all;


    }

    /*@Override
    public Setmeal findCheckItemCheckGroupSetmealById(Integer id) {

        Setmeal setmeal = setmealDao.findCheckItemCheckGroupSetmealById(id);

        return setmeal;
    }*/

    @Override
    public Setmeal findCheckItemCheckGroupSetmealById(Integer id) {

        Setmeal setmeal = setmealDao.findCheckItemCheckGroupSetmealById(id);

        return setmeal;
    }

    @Override
    public List<Map<String, Object>> findSetmealCount() {

        return setmealDao.findSetmealCount();
    }

    @Override
    public List<Integer> findSetmealIdByOrderId(Integer id) {
        return null;
    }
}
